
//
//  CourseCenterViewController.m
//  ShowYork
//
//  Created by hxh on 2020/8/30.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonCenterViewController.h"
#import "LessonCenterItemViewController.h"
#import "LessonSearchResultViewController.h"
#import "LessonItemViewController.h"
#import "LessonSearchResultViewController.h"
#import "LessonSearchFlowLayout.h"
#import "LessonCategoryItemCell.h"
#import "LessonCenterMenuCell.h"
#import "LessonSearchModel.h"
#import "LessonPageItemModel.h"
#import "LessonCategoryModel.h"
#import "LessonCenterNavView.h"

#import "LessonSegmentedControl.h"

@interface LessonCenterViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)LessonCenterNavView *nav;
@property (nonatomic,strong)LessonSegmentedControl *segmentedControl;

@property (nonatomic,strong)NSIndexPath *indexPath;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,copy)NSArray *dataArr;
@property (nonatomic,copy)NSArray *searchArray;
@property (nonatomic,copy)NSArray *sourceArray;

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView *statusBar;


@end

@implementation LessonCenterViewController

-(NSArray *)dataArr{
    if(!_dataArr){
        _dataArr = @[@"语文",@"数学",@"英语"];
    }
    return _dataArr;
}

- (NSIndexPath *)indexPath{
    if (!_indexPath) {
        _indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    }
    return _indexPath;
}

- (NSArray *)sourceArray{
    if (!_sourceArray) {
        _sourceArray = [NSArray array];
    }
    return _sourceArray;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}




- (instancetype)initWithSeachArray:(NSArray *)searchArray andCurrentIndex:(NSInteger)index{
    if (self = [super init]) {
        self.index = index;
        self.indexPath = [NSIndexPath indexPathForRow:index-1 inSection:0];
        self.searchArray = searchArray;
    }
    return self;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = YES;
    [IQKeyboardManager sharedManager].enable = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self updateStatusBarColor:RGBA(248, 248, 248, 1)];
  
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = NO;
    [IQKeyboardManager sharedManager].enable = YES;
    [self updateStatusBarColor:UIColor.clearColor];
}

- (void)updateStatusBarColor:(UIColor *)color{
    if (@available(iOS 13.0, *)) {
        self.statusBar.backgroundColor = color;
    } else {
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = color;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHideNavBar = YES;
    
    [self setUI];
    
}

- (void)setUI{
    
    if (@available(iOS 13.0, *)) {
        self.statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];
        [[UIApplication sharedApplication].keyWindow addSubview:self.statusBar];
    }
    
    
    // 设置头部搜索栏
    LessonSearchModel *model = [self.searchArray firstObject];
    self.nav = [[LessonCenterNavView alloc]init];
    [self.nav setNavTitle:model.word];
    [self.view addSubview:self.nav];
    
    [self.nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(GK_STATUSBAR_HEIGHT);
        make.height.mas_equalTo(44);
    }];

    __weak typeof(self)weakSelf = self;
    self.nav.done = ^{
        
        [weakSelf updateStatusBarColor:UIColor.clearColor];
        
        __strong typeof(weakSelf)strongSelf = weakSelf;
        LessonSearchResultViewController *searchVC = [[LessonSearchResultViewController alloc]init];
        searchVC.hotArray = strongSelf.searchArray;
        [strongSelf.navigationController pushViewController:searchVC animated:YES];
    };
    self.nav.back = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    // 设置分页指示器
    self.segmentedControl = [[LessonSegmentedControl alloc]initWithSourceTitles:self.dataArr completeBlock:^(NSInteger index) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf.scrollView setContentOffset:CGPointMake(kScreenW * index, 0) animated:YES];
    }];
    [self.segmentedControl showInView:self.view];
    [self.segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.view).offset(GK_STATUSBAR_HEIGHT + 44.0f);
    }];
    // 设置分页指示器
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.top.equalTo(self.view).offset(GK_STATUSBAR_HEIGHT + 88.0f);
    }];
//    // 设置分页数量
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.scrollView setContentSize:CGSizeMake(kScreenW * self.dataArr.count, 0)];
    [self.scrollView setContentOffset:CGPointMake(kScreenW * (self.index - 1), 0) animated:YES];

    self.segmentedControl.currentIndex = self.index - 1;

    [self.dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        LessonCenterItemViewController *itemVC = [[LessonCenterItemViewController alloc]initWithItemType:idx + 1];
        [strongSelf.scrollView addSubview:itemVC.view];
        [strongSelf addChildViewController:itemVC];
    }];

    __block UIView *lastView = nil;

    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (lastView == nil) {
            [obj mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.height.width.equalTo(self.scrollView);
                make.leading.mas_equalTo(self.scrollView.mas_leading);
            }];
        }else{
            [obj mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.height.width.equalTo(self.scrollView);
                make.leading.mas_equalTo(lastView.mas_trailing);
            }];
        }
        lastView = obj;
    }];
//
}


#pragma mark - 隐藏分割线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
     NSInteger index = scrollView.contentOffset.x/kScreenW;
     
     self.segmentedControl.currentIndex = index;
}


@end
